<!-- 回复整改日期弹窗 -->
<template>
  <px-dialog v-model="visible" class="equip-dialog-wrap" title="回复整改日期" width="480px" @open="openDialog" @close="closeDialog">
    <px-form ref="formRef" :model="formValue" :rules="rules" label-position="right" label-width="120px">
      <px-row>
        <px-col :span="24">
          <px-form-item label="计划整改日期" prop="planRectifyTime">
            <px-date-picker
              style="width: 100%"
              v-model="formValue.planRectifyTime"
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="请输入计划整改日期"
            />
          </px-form-item>
        </px-col>
      </px-row>
    </px-form>
    <div style="margin-top: 56px; text-align: right">
      <px-button @click="closeDialog">取消</px-button>
      <px-button type="primary" @click="handleSubmit(formRef)">确定</px-button>
    </div>
  </px-dialog>
</template>

<script setup lang="ts">
import { computed, reactive, ref, toRefs } from "vue";
import { PxMessage } from "px-ui";
import { replyRectifyDateDefect } from "@/api/defect";

// 调用父级-关闭弹窗、查询列表方法
const emits = defineEmits(["closeDialog"]);
// 父级传过来的值
const props = defineProps(["dialogVisible", "missingId"]);

const visible = computed(() => props.dialogVisible);
// 页面本身的值
const formRef = ref();
const state = reactive<any>({
  formValue: {
    planRectifyTime: ""
  }
});
const { formValue } = toRefs(state);

// 表单校验规则
const rules = computed(() => ({
  planRectifyTime: [{ required: true, message: "计划整改日期不能为空", trigger: "change" }]
}));

// 打开弹窗
const openDialog = async () => {
  formRef.value.resetFields();
  const userInfo = JSON.parse(localStorage.getItem("userInfo"));
  formValue.value.responseUser = userInfo.username + "-" + userInfo.realName;
};
// 关闭弹窗
const closeDialog = () => {
  emits("closeDialog");
};
// 提交
const handleSubmit = async (formEl: any) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      const params = {
        missingId: props.missingId,
        responseUser: formValue.value.responseUser,
        planRectifyTime: formValue.value.planRectifyTime + " 23:59:59"
      };
      await replyRectifyDateDefect(params);
      PxMessage.success("回复整改日期成功");
      emits("closeDialog");
    }
  });
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/add-dialog.scss");
</style>
